package com.lebang.controller.api.donwn;

import com.lebang.domain.TaskEarn;
import com.lebang.entity.RequestHeaders;
import com.lebang.mapper.LbbMemberRecordMapper;
import com.lebang.service.TaskInRedisService;
import com.lebang.service.WebApiService;
import com.lebang.service.sys.TaskEarnService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

import static com.lebang.component.CommonComponent.TASK_COMMON_AMOUNT;

/**
 * @Author: hwq
 * @Date: 2020/6/9 11:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/down/taskearn")
public class TaskEranApi {
    @Autowired
    private TaskEarnService taskEarnService;
    @Autowired
    private TaskInRedisService taskInRedisService;
    @Autowired
    private LbbMemberRecordMapper lbbMemberRecordMapper;

    @PostMapping("/rv")
    public JsonData rv(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        List<TaskEarn> list = taskEarnService.selectAllList();
        List<String> memberTaskList = lbbMemberRecordMapper.getAllMemberTaskList(requestHeaders.getUserid());
        Collections.shuffle(list);
        for (TaskEarn next : list) {
            int i = taskInRedisService.getTaskAmount(TASK_COMMON_AMOUNT, next.getTaskId());
            if (!memberTaskList.contains(next.getTaskId())) {
                if (i > 0) {
                    return Callback.success("success", "查询成功", next);
                } else {
                    next.setEnable(0);
                    taskEarnService.updateById(next);
                }
            }
        }
        return Callback.fail("200", "没有任务了");
    }
}
